package cn.edu.gduf.luowowo.common.util;

import org.joda.time.DateTime;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 获取两个时间的间隔(秒)
     *
     * @param d1
     * @param d2
     * @return
     */
    public static long getDateBetween(Date d1, Date d2) {
        return Math.abs((d1.getTime() - d2.getTime()) / 1000);//取绝对值
    }

    public static Date getEndDate(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        return c.getTime();
    }

    public static List<String> getListDateSting(int day) {
        List<String> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        list.add(sdf.format(new Date()));
        for (int i = 0; i < day; i++) {
            calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)-1);
            String stringDate = sdf.format(calendar.getTime());
            list.add(stringDate);
        }
        list.remove(list.size()-1);
        return list;
    }

    public static void main(String[] args) {
        for (String s : getListDateSting(30)) {
            System.out.println(s);
        }
    }


}
